package cn.itsource.pethome.product.service.impl;

import cn.itsource.pethome.basic.service.impl.BaseServiceImpl;
import cn.itsource.pethome.product.constant.ProductConstant;
import cn.itsource.pethome.product.domain.Product;
import cn.itsource.pethome.product.domain.ProductDetail;
import cn.itsource.pethome.product.mapper.ProductDetailMapper;
import cn.itsource.pethome.product.mapper.ProductMapper;
import cn.itsource.pethome.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.SavepointManager;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * 产品服务相关服务实现层
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService{
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;

    /**
     * 保存产品服务信息和详情
     *
     * @param product
     */
    @Override
    @Transactional
    public void save(Product product) {
        //默认下架状态
        product.setState(ProductConstant.PRODUCT_STATE);
        //服务产品销量
        product.setSalecount(ProductConstant.PRODUCT_SALE_COUNT);
        super.save(product);
        ProductDetail productDetail = product.getProductDetail();
        productDetail.setProductId(product.getId());
        //更新服务产品信息详情
        productDetailMapper.saveProductDetail(productDetail);
    }

    /**
     * 上架
     *
     * @param ids
     */
    @Override
    @Transactional
    public void patchPut(Long[] ids) {
        //创建产品对象
        Product product = new Product();
        //循环遍历
        for (Long id : ids) {
            product.setOnsaletime(new Date());
            //调用更新方法
            productMapper.updateStateById(id,ProductConstant.PRODUCT_ACTIVE,product);
        }
    }

    /**
     * 下架
     *
     * @param ids
     */
    @Override
    @Transactional
    public void patchDown(Long[] ids) {
        //创建产品对象
        Product product = new Product();
        for (Long id : ids) {
            product.setOffsaletime(new Date());
            //调用更新方法
            productMapper.updateStateById(id,ProductConstant.PRODUCT_STATE,product);
        }
    }

    /**
     * 修改
     * @param product
     */
    @Override
    @Transactional
    public void update(Product product) {
        super.update(product);

        ProductDetail productDetail = product.getProductDetail();
        productDetail.setProductId(product.getId());
        productDetailMapper.updateDetail(productDetail);
    }


    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Long id) {
        //删除服务信息
        super.delete(id);
        //删除服务详细
        productDetailMapper.delete(id);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void patchDelete(Long[] ids) {
        //删除服务信息
        super.patchDelete(ids);
        //删除服务详细
        productDetailMapper.patchDelete(ids);

    }
}
